/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package idv.cloudee.proxy2.test;

import idv.cloudee.proxy2.impl.getpost.GETPOSTModifier;
import com.cirnoworks.http.utils.HeaderRequest;
import com.cirnoworks.http.utils.HeaderResponse;
import idv.cloudee.proxy2.framework.ProxySession;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map.Entry;

/**
 *
 * @author Cloudee
 */
public class GETPOSTLogger implements GETPOSTModifier {

    private static int seq = 0;
    private static long startTime = System.currentTimeMillis();
    private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static synchronized int getSeq() {
        seq++;
        return seq;
    }

    public static synchronized void reset() {
        startTime = System.currentTimeMillis();
        seq = 0;
    }
    private PrintStream fout;

    public void beginRequest(ProxySession session) {
        try {
            File dir = new File("log/" + startTime + "/");
            dir.mkdirs();
            fout = new PrintStream("log/" + startTime + "/" + getSeq() + ".log");
        } catch (IOException ex) {
            ex.printStackTrace();
            System.err.println("Log fail!");
            System.exit(-1);
        }
        fout.print("=======Requsst======");
        fout.print(sdf.format(new Date()));
        fout.print(" ");
        fout.println(hashCode());
    }

    public HeaderRequest modifyRequest(HeaderRequest param) {
        fout.println(param.getRequestLine());
        param.clear("Accept-Encoding");
        String key;
        for (Entry<String, ArrayList<String>> entry : param.entrySet()) {
            key = entry.getKey();
            for (String value : entry.getValue()) {
                fout.println(key + ": " + value);
            }
        }
        fout.println();
        return param;
    }

    public OutputStream getPOSTOutputStream(OutputStream os) {
        return new DisplayOutputStream(os);
    }

    public HeaderResponse modifyResponse(HeaderResponse param) {
        fout.println("=======Response======");
        fout.println(param.getResponseLine());
        String key;
        for (Entry<String, ArrayList<String>> entry : param.entrySet()) {
            key = entry.getKey();
            for (String value : entry.getValue()) {
                fout.println(key + ": " + value);
            }
        }
        fout.println();
        return param;
    }

    public OutputStream getResponseOutputStream(OutputStream os) {
        return new DisplayOutputStream(os);
    }

    public void requestOver() {
        fout.println("=========Closed=========");
        fout.close();
    }

    class DisplayOutputStream extends OutputStream {

        private OutputStream os;

        public DisplayOutputStream(OutputStream os) {
            this.os = os;
        }

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            os.write(b, off, len);
            fout.write(b, off, len);
        }

        @Override
        public void write(byte[] b) throws IOException {
            os.write(b);
            fout.write(b);
        }

        public void write(int b) throws IOException {
            os.write(b);
            fout.write(b);
        }

        @Override
        public void flush() throws IOException {
            os.flush();
            fout.flush();
        }

        @Override
        public void close() throws IOException {
            os.close();
        }
    }
}
